package com.zwc.angle2048;

import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.zwc.angle.AngleActivity;
import com.zwc.angle.AngleFont;
import com.zwc.angle.AngleObject;
import com.zwc.angle.AngleSprite;
import com.zwc.angle.AngleSpriteLayout;
import com.zwc.angle.AngleString;
import com.zwc.angle.AngleUI;

/**
 * 高分榜
 * 
 * @author zwc
 *
 */
public class UIOfHiScore extends AngleUI {
	private int width = GameUtil.WIDTH;
	private int height = GameUtil.HEIGHT;
	private String[] strNo = { "1", "2", "3", "4", "5", "last" };
	private String[][] data = new String[6][3];

	private AngleObject groupOfBg;
	private AngleObject groupOfScores;
	private AngleObject groupOfTexts;
	private AngleString[][] Scores = new AngleString[6][3];
	private AngleString strBack;
	private AngleString strClassic;
	private AngleString strTime;
	private AngleString strUnlimited;
	private AngleString strTitle;

	@Override
	public void onActivate() {
		change("Classic");
		super.onActivate();
	}
	/**
	 * 布局设置
	 * 
	 * @param activity
	 */
	public UIOfHiScore(AngleActivity activity) {
		super(activity);
		groupOfBg = new AngleObject();
		groupOfScores = new AngleObject();
		groupOfTexts = new AngleObject();
		addObject(groupOfBg);
		addObject(groupOfScores);
		addObject(groupOfTexts);

		AngleFont font_HoneyLight = new AngleFont(mActivity.mGLSurfaceView, 50,
				Typeface.createFromAsset(mActivity.getAssets(),
						"HoneyLight.ttf"), 222, 0, 0, 30, 200, 255, 255);
		AngleFont font_Almost_Cartoon = new AngleFont(mActivity.mGLSurfaceView,
				40, Typeface.createFromAsset(mActivity.getAssets(),
						"Almost_Cartoon.ttf"), 222, 0, 0, 0, 0, 0, 255);
		AngleFont fntAddElectricCity = new AngleFont(mActivity.mGLSurfaceView,
				60, Typeface.createFromAsset(mActivity.getAssets(),
						"AddElectricCity.ttf"), 200, 0, 0, 255, 0, 0, 255);
		AngleFont font_bazaronitet = new AngleFont(mActivity.mGLSurfaceView,
				40, Typeface.createFromAsset(mActivity.getAssets(),
						"bazaronite.ttf"), 222, 0, 0, 0, 0, 0, 255);

		groupOfBg.addObject(new AngleSprite(width / 2, height / 2,
				new AngleSpriteLayout(mActivity.mGLSurfaceView, width, height,
						com.zwc.angle2048.R.drawable.bg)));

		groupOfTexts.addObject(new AngleString(font_Almost_Cartoon, "No",
				width / 5, height / 3, AngleString.aCenter));
		for (int i = 0; i < 6; i++) {
			groupOfTexts.addObject(new AngleString(font_bazaronitet, strNo[i],
					width / 5, height / 3 + (i + 1) * 60, AngleString.aCenter));
		}
		groupOfTexts.addObject(new AngleString(font_Almost_Cartoon, "Score",
				width * 2 / 5, height / 3, AngleString.aCenter));
		groupOfTexts.addObject(new AngleString(font_Almost_Cartoon, "Moves",
				width * 3 / 5, height / 3, AngleString.aCenter));
		groupOfTexts.addObject(new AngleString(font_Almost_Cartoon, "Time",
				width * 4 / 5, height / 3, AngleString.aCenter));
		strTitle = (AngleString) groupOfTexts.addObject(new AngleString(
				font_Almost_Cartoon, "Classic", width / 2, height / 3 - 65,
				AngleString.aCenter));
		strClassic = (AngleString) groupOfTexts.addObject(new AngleString(
				fntAddElectricCity, "C", width / 4, height / 6,
				AngleString.aCenter));
		strTime = (AngleString) groupOfTexts.addObject(new AngleString(
				fntAddElectricCity, "T", width / 2, height / 6,
				AngleString.aCenter));
		strUnlimited = (AngleString) groupOfTexts.addObject(new AngleString(
				fntAddElectricCity, "U", width * 3 / 4, height / 6,
				AngleString.aCenter));
		strBack = (AngleString) groupOfTexts.addObject(new AngleString(
				font_HoneyLight, "BACK", width / 2, height * 9 / 10 - 30,
				AngleString.aCenter));

		show("Classic");
	}

	/**
	 * 获取数据
	 * 
	 * @param mode
	 * @return
	 */
	private String[][] getData(String mode) {
		String[][] d = new String[6][3];
		for (int i = 0; i < 6; i++) {
			String key = mode + "Score" + (i + 1);
			String temp = (String) GameDB.getParam(mActivity, key,
					"1000_100_123");
			String[] s = GameUtil.toStrArr(temp);
			for (int j = 0; j < 3; j++) {
				if (j == 2)
					s[j] = GameUtil.secToTime(Integer.parseInt(s[j]));
				d[i][j] = s[j];
			}
		}
		return d;
	}

	/**
	 * 显示数据
	 * 
	 * @param mode
	 */
	private void show(String mode) {
//		System.out.println("show" + mode);
		AngleFont font_bazaronitet = new AngleFont(mActivity.mGLSurfaceView,
				40, Typeface.createFromAsset(mActivity.getAssets(),
						"bazaronite.ttf"), 222, 0, 0, 0, 0, 0, 255);
		strTitle.set(mode);
		data = getData(mode);
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 3; j++) {
				Scores[i][j] = new AngleString(font_bazaronitet, data[i][j],
						width * (j + 2) / 5, height / 3 + ((i + 1) * 60),
						AngleString.aCenter);
				groupOfScores.addObject(Scores[i][j]);
			}
		}
	}

	/**
	 * 改变显示的数据
	 * 
	 * @param mode
	 */
	private void change(String mode) {
		strTitle.set(mode);
		data = getData(mode);
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 3; j++) {
				Scores[i][j].set(data[i][j]);
			}
		}
	}

	/**
	 * 触摸事件
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (strClassic.test(event.getX(), event.getY())) {
				change("Classic");
			} else if (strTime.test(event.getX(), event.getY())) {
				change("Time");
			} else if (strUnlimited.test(event.getX(), event.getY())) {
				change("Unlimited");
			} else if (strBack.test(event.getX(), event.getY())) {
				((MainActivity) mActivity)
						.setUI(((MainActivity) mActivity).mTheMenu);
			}
			break;
		}
		return true;
	}

	/**
	 * 手机右键跳转上一界面
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			((MainActivity) mActivity)
					.setUI(((MainActivity) mActivity).mTheMenu);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
